﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;

namespace RealtorsPortal.AdminCP
{
    public partial class ManageAccounts : System.Web.UI.Page
    {
        AccountDAL ad = new AccountDAL();
        static Account a = new Account();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                RoleDAL rd = new RoleDAL();
                ddlRolesFilter.DataSource = ddlRoles.DataSource = rd.GetRoleDataTable();
                ddlRolesFilter.DataTextField = ddlRoles.DataTextField = "RoleName";
                ddlRolesFilter.DataValueField = ddlRoles.DataValueField = "RoleId";
                ddlRoles.DataBind();
                ddlRolesFilter.DataBind();
                this.LoadAndBindData();
            }
            
        }

        private void LoadAndBindData()
        {
            int rolefilter = int.Parse(ddlRolesFilter.SelectedValue);
            grvAccounts.DataSource = ad.GetAccountsDataTableByRoleId(rolefilter);
            grvAccounts.DataBind();
        }

        protected void grvAccounts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvAccounts.PageIndex = e.NewPageIndex;
            this.LoadAndBindData();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvAccounts.PageSize = int.Parse(ddlPageSize.Text);
            this.LoadAndBindData();
        }

        protected void grvAccounts_RowEditing(object sender, GridViewEditEventArgs e)
        {

            int index = e.NewEditIndex;
            int userId =int.Parse(grvAccounts.DataKeys[index].Value.ToString());
            a = ad.GetAccountById(userId);
            lblAccountId.Text = a.AccountId.ToString();
            lblRegDate.Text = a.RegDate;
            lblRole.Text = a.Role.RoleName;
            txtUserName.Text = a.Username;
            txtEmail.Text = a.Email;
            txtFirstName.Text = a.FirstName;
            txtLastName.Text = a.LastName;
            txtPaypalAccount.Text = a.PayPalAccount;
            txtCompany.Text = a.Company;
            txtPhone.Text = a.Phone;
            txtAddress.Text = a.Address;
            chkIsActive.Checked = a.IsActive;
            AccountEditPanel.Visible = true;
        }

        protected void grvAccounts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int userId = int.Parse(grvAccounts.DataKeys[e.RowIndex].Value.ToString());
            ad.DeleteAccount(userId);
            this.LoadAndBindData();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            a.Username = txtUserName.Text;
            a.Email = txtEmail.Text;
            a.FirstName = txtFirstName.Text;
            a.LastName = txtLastName.Text;
            a.PayPalAccount = txtPaypalAccount.Text;
            a.Company = txtCompany.Text;
            a.Phone = txtPhone.Text;
            a.Address = txtAddress.Text;
            a.IsActive = chkIsActive.Checked;

            if (ad.UpdateAccount(a))
            {
                //do something
                this.LoadAndBindData();
                AccountEditPanel.Visible = false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Account acc = new Account();
            acc.Role = new Role();
            acc.Role.RoleId = int.Parse(ddlRoles.SelectedValue);
            acc.Username = atxtUsername.Text;
            acc.Password = atxtPassword.Text;
            acc.Email = atxtEmail.Text;
            acc.FirstName = atxtFirstName.Text;
            acc.LastName = atxtLastName.Text;
            acc.PayPalAccount = atxtPaypal.Text;
            acc.Company = atxtCompany.Text;
            acc.Phone = atxtPhone.Text;
            acc.Address = atxtAddress.Text;
            acc.IsActive = true;
            acc.RegDate = DateTime.Now.ToShortDateString();
            if (ad.AddAccount(acc))
            {
                //do something
            }
        }

        protected void ddlRolesFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadAndBindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string key = txtKeyword.Text.Trim();
            int roleId = int.Parse(ddlRolesFilter.SelectedValue);
            grvAccounts.DataSource = ad.SearchAccountByUserName(key, roleId);
            grvAccounts.DataBind();
        }
    }
}